<?php


namespace App\Http\Composers;


use App\Handlers\DeviceTypeHandlers;

use App\Repositories\ArticleCateRepository;
use App\Repositories\BannerRepository;

use App\Repositories\NavigationRepository;
use App\Repositories\SeoRepository;

use App\Repositories\TeamRepository;
use App\Services\ConfigService;
use Illuminate\View\View;
use Illuminate\Support\Facades\DB;
class LayoutComposer
{
    private $view;

    private $uri;

    private $path;

    private $data = [];


    public function all(View $view){

        $this->view = $view;

        $this->path = request()->path();

        if(request()->route()){
            $this->uri = request()->route()->uri();

        }

        $this->seo();

        $this->getArticleCate();

        $this->getBanners();

        $this->getTeam();

        $this->view->with('layout',$this->data);
    }

    /**
     * 獲取seo三大標簽
     */
    protected function seo(){

        $seo = app(SeoRepository::class)->findPath('/'.trim($this->path,'/'));
        if(!$seo){
            $seo = app(SeoRepository::class)->findPath('/'.trim($this->uri,'/'));
        }

        if($seo && $seo->title_tail == 1){
            $seo->title = $seo->title.ConfigService::get('seo_title_tail');
        }
        $this->data['seo'] = $seo;

    }


    public function getArticleCate(){
        $this->data['global-article-cate'] = app(ArticleCateRepository::class)->getAll();
    }


    public function getBanners(){

        if(request()->is('team-detail/*') || request()->is('team-about/*')){
            $id = request()->route()->parameter('id');
            $team = app(TeamRepository::class)->find($id);
            $banners = $team->banners;
        }elseif (request()->is('recruit')){
            $page_banners = app('cache.config')->get('page_recruit_banners');
            $banners = explode(',',$page_banners);
        }else{
            $page_banners = app('cache.config')->get('page_banners');
            $banners = explode(',',$page_banners);
        }

        shuffle($banners);

        $this->data['banners'] = $banners;


    }

    public function getTeam(){
        $this->data['teams'] = app(TeamRepository::class)->get();

    }

}
